// myself/daily/WorkList.js
import api from "../../utils/api.js";
import wechat from '../../utils/wechat.js';
import network from '../../utils/network.js';
import formatDate from "../../utils/format_date.js";
import notification from '../../utils/notification_center.js';
const app = getApp();
let nowPage = 0;
let maxPage = 1;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    list: [],
    page: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    notification.addNotification("DailyWorkList", this.refreshNotification, this);
    this.toast = this.selectComponent("#toast");
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    notification.removeNotification("DailyWorkList", this);
    nowPage = 0;
    maxPage = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.request();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.request();
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */
  request: function () {
    if (nowPage == maxPage) {
      this.toast.showToast("暂无更多");
    } else {
      let param = {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,
        loadci: 1,
        storebeforeaction: 'storebeforeshow',
        storeafteraction: 'storeaftershow',
        modenum:'daily',
        atype:'my',
        page: this.data.page,
      }
      network.postJson(this, api.fwork, param).then(res => {

        
        if (res.data.data.rows){
          for (let i = 0; i < res.data.data.rows.length; i++) {
            res.data.data.rows[i]['adddt']=res.data.data.rows[i]['adddt'].replace("<br>", " ")
          }
        }
        this.setData({
          [`list[${nowPage}]`]: res.data.data.rows,
          page: res.data.data.nextpage
        })

        nowPage = res.data.data.page;
        maxPage = res.data.data.maxpage;
      });
    }
  },

  refreshNotification: function () {
    this.onPullDownRefresh();
  },
  goAddwork(){
    wx.navigateTo({
      url: '/myself/daily/Work'
    })
  },
  goWordDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/myself/daily/WorkDetails?id=' + id
    })
  }
})